home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / debug / common / dbgcom.c next >
Encoding:
C/C++ Source or Header  |  1996-01-11  |  9.4 KB  |  339 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <dpmi.h>
  9. #include <crt0.h>
  10. #include <go32.h>
  11. #include <signal.h>
  12. #include <setjmp.h>
  13. #include <debug/dbgcom.h>
  14. #include <sys/exceptn.h>
  15.  
  16. extern char __libdbg_ident_string[];
  17. static char *id = __libdbg_ident_string;
  18.  
  19. /* ARGSUSED */
  20. char **
  21. __crt0_glob_function(char *foo)
  22. {
  23.   id = 0;
  24.   return 0;
  25. }
  26.  
  27. ExternalDebuggerInfo edi;
  28. TSS a_tss;
  29. static jmp_buf jumper;
  30.  
  31. static int my_ds,my_cs,app_cs;
  32. static jmp_buf load_state;
  33.  
  34. static int nset, breakhandle[4];
  35.  
  36. static int _DPMIsetBreak(unsigned short sizetype, unsigned vaddr)
  37. {
  38.     int handle;
  39.  
  40.     asm volatile(                               "\n\
  41.         movw   %1,%%dx                        \n\
  42.         movl   %2,%%ecx                        \n\
  43.         movl   %%ecx,%%ebx                        \n\
  44.         shrl   $16,%%ebx                        \n\
  45.         movw   $0x0b00,%%ax                        \n\
  46.         int    $0x31                        \n\
  47.         jnc    3f                            \n\
  48.         xorl   %%ebx,%%ebx                        \n\
  49.         decl   %%ebx                        \n\
  50.         jmp    1f                            \n\
  51. 3:          movzwl %%bx,%%ebx                        \n\
  52. 1:        movl   %%ebx,%0                        \n\
  53.         "
  54.         : "=g" (handle)            /* outputs */
  55.         : "g" (sizetype), "g"  (vaddr)    /* inputs */
  56.         : "ax", "bx", "cx", "dx"        /* regs used */
  57.     );
  58.     return handle;
  59. }
  60.  
  61. static int _DPMIcancelBreak(int handle)
  62. {
  63.     unsigned state;
  64.  
  65.     asm volatile(                               "\n\
  66.         movl   %1,%%ebx                        \n\
  67.         movw   $0x0b02,%%ax                        \n\
  68.         int    $0x31                        \n\
  69.         jnc    2f                            \n\
  70.         xorl   %%eax,%%eax                                          \n\
  71. 2:        andl   $1,%%eax                        \n\
  72.         pushl  %%eax                        \n\
  73.         movw   $0x0b01,%%ax                        \n\
  74.         int    $0x31                        \n\
  75.         popl   %0                            \n\
  76.         "
  77.         : "=g" (state)            /* outputs */
  78.         : "g"  (handle)            /* inputs */
  79.         : "ax", "bx"            /* regs used */
  80.     );
  81.     return state;
  82. }
  83.  
  84. /* Can't be static because called in asm below; -O3 inlines if static */
  85. void _set_break_DPMI(void);
  86. void _set_break_DPMI(void)
  87. {
  88.   int i;
  89.   unsigned extract;
  90.   unsigned short sizetype;
  91.   unsigned long vbase;
  92.   
  93.   if(__dpmi_get_segment_base_address(__djgpp_app_DS, &vbase) == -1)
  94.     return;
  95.   extract = edi.dr[7] >> 16;
  96.   nset = 0;
  97.  
  98.   for(i=0;i<4;i++)
  99.     if( (edi.dr[7] >> (i*2))&3 ) {        /* enabled? */
  100.       sizetype = (extract >> (i*4)) & 3;    /* extract the type */
  101.       if(sizetype == 3) sizetype = 2;       /* convert for DPMI brain damage */
  102.       sizetype = (sizetype << 8) + ((extract >> (i*4+2)) & 3) + 1; /* & size */
  103.       breakhandle[i] = _DPMIsetBreak(sizetype, edi.dr[i]+vbase);
  104.       if(breakhandle[i] == -1)
  105.         printf("Error allocating DPMI breakpoint at address 0x%08lx\n",edi.dr[i]);
  106.       else
  107.         nset++;
  108.     } else
  109.       breakhandle[i] = -1;
  110.   return;
  111. }
  112.  
  113. /* Can't be static because called in asm below; -O3 inlines if static */
  114. void _clear_break_DPMI(void);
  115. void _clear_break_DPMI(void)
  116. {
  117.   int i,bt;
  118.  
  119.   if(!nset) {
  120.     edi.dr[6] = 0;
  121.     return;
  122.   }
  123.  
  124.   bt = 0;
  125.   for(i=3;i>=0;i--) {
  126.     bt = bt << 1;                             /* Shift for next bit */
  127.     if(breakhandle[i] != -1)
  128.       bt |= _DPMIcancelBreak(breakhandle[i]);  /* Set low bit if active */
  129.   }
  130.  
  131.   edi.dr[6] = bt;
  132. }
  133.  
  134. static __dpmi_paddr old_i31,old_i21;
  135.  
  136. static void hook_dpmi(void)
  137. {
  138.   __dpmi_paddr new_int;
  139.   extern void i21_hook(void),i31_hook(void);
  140.  
  141.   __dpmi_get_protected_mode_interrupt_vector(0x21, &old_i21);
  142.   __dpmi_get_protected_mode_interrupt_vector(0x31, &old_i31);
  143.  
  144.   asm("mov %%cs,%0" : "=g" (new_int.selector) );
  145.   new_int.offset32 = (unsigned long)i21_hook;
  146.   __dpmi_set_protected_mode_interrupt_vector(0x21, &new_int);
  147.   new_int.offset32 = (unsigned long)i31_hook;
  148.   __dpmi_set_protected_mode_interrupt_vector(0x31, &new_int);
  149. }
  150.  
  151. /* BUGS: We ignore the exception handlers for the child process, so signals
  152.    do not work.  We also disable the hooking of HW interrupts that might
  153.    cause the HW-interrupt-to-limit exceptions, since they can never be fixed.
  154.    Byproduct:  You can't debug code which hooks int 9, since it's keyboard
  155.    routine never gets called.  Eventually, we should save the exception and
  156.    interrupt hooks and then chain to them on the next execution.  Someday. */
  157.  
  158. /* Watch set selector base, if it is __djgpp_app_DS then reset breakpoints */
  159. asm(                                           "\n\
  160.     .text                                \n\
  161.     .align  2,0x90                            \n\
  162. _i31_hook:                                \n\
  163.     cmpw    $0x0203,%ax                        \n\
  164.     je    Lc31a                            \n\
  165.     cmpw    $0x0007,%ax                        \n\
  166.     je    Lc31b                            \n\
  167.     cmpw    $0x0205,%ax                        \n\
  168.     je    Lc31d                            \n\
  169. Lc31c:    .byte    0x2e                            \n\
  170.     ljmp    _old_i31                        \n\
  171. Lc31a:    iret                                \n\
  172. Lc31b:    .byte    0x2e                            \n\
  173.     cmpw    ___djgpp_app_DS,%bx                    \n\
  174.     jne    Lc31c                            \n\
  175.     pushf                                \n\
  176.     .byte    0x2e                            \n\
  177.     lcall    _old_i31                        \n\
  178.     call    ___djgpp_save_interrupt_regs                \n\
  179.     call    __clear_break_DPMI                    \n\
  180.     call    __set_break_DPMI                    \n\
  181.     movl    ___djgpp_exception_state_ptr,%eax            \n\
  182.     pushl    (%eax)                            \n\
  183.     pushl    %eax                            \n\
  184.     call    _longjmp                        \n\
  185. Lc31d:    cmpb    $9,%bl                            \n\
  186.     je    Lc31a                            \n\
  187.     cmpb    $0x75,%bl                        \n\
  188.     je    Lc31a                            \n\
  189.     jmp    Lc31c                            \n\
  190.     .align  2,0x90                            \n\
  191. _i21_hook:                                \n\
  192.     cmpb    $0x4c,%ah                        \n\
  193.     je    Lc21                            \n\
  194. Lc21j:    .byte    0x2e                            \n\
  195.     ljmp    _old_i21                        \n\
  196. Lc21:    push    %eax                            \n\
  197.     movl    8(%esp),%eax                        \n\
  198.     cs                                \n\
  199.     cmpw    _app_cs,%ax                        \n\
  200.     pop    %eax                            \n\
  201.     jg    Lc21j                            \n\
  202.     call    ___djgpp_save_interrupt_regs                \n\
  203.     movl    ___djgpp_exception_state_ptr,%esi            \n\
  204.     movl    $0x21,56(%esi)                        \n\
  205.     movl    $_load_state,%edi                    \n\
  206.     movl    $43,%ecx                        \n\
  207.     rep                                \n\
  208.     movsl                                \n\
  209.     pushl    $1                            \n\
  210.     pushl    $_jumper                        \n\
  211.     call    _longjmp                        \n\
  212.     "
  213.     );
  214.  
  215. /*    movw    %cs:__go32_info_block+26, %fs                \n\
  216.     .byte    0x64                            \n\
  217.     movw    $0x7021,0xb0f00                        \n\ */
  218.  
  219. static void unhook_dpmi(void)
  220. {
  221.   __dpmi_set_protected_mode_interrupt_vector(0x31, &old_i31);
  222.   __dpmi_set_protected_mode_interrupt_vector(0x21, &old_i21);
  223. }
  224.  
  225. static void dbgsig(int sig)
  226. {
  227.   if(__djgpp_exception_state->__cs != my_cs || sig == SIGTRAP) {
  228.     *load_state = *__djgpp_exception_state;    /* exception was in other process */
  229.     longjmp(jumper, 1);
  230.   }
  231. }
  232.  
  233. void run_child(void)
  234. {
  235.   load_state->__cs = a_tss.tss_cs;
  236.   load_state->__ss = a_tss.tss_ss;
  237.   load_state->__ds = a_tss.tss_ds;
  238.   load_state->__es = a_tss.tss_es;
  239.   load_state->__fs = a_tss.tss_fs;
  240.   load_state->__gs = a_tss.tss_gs;
  241.   load_state->__eip = a_tss.tss_eip;
  242.   load_state->__eflags = a_tss.tss_eflags;
  243.   load_state->__eax = a_tss.tss_eax;
  244.   load_state->__ebx = a_tss.tss_ebx;
  245.   load_state->__ecx = a_tss.tss_ecx;
  246.   load_state->__edx = a_tss.tss_edx;
  247.   load_state->__esp = a_tss.tss_esp;
  248.   load_state->__ebp = a_tss.tss_ebp;
  249.   load_state->__esi = a_tss.tss_esi;
  250.   load_state->__edi = a_tss.tss_edi;
  251.   if(!setjmp(jumper)){
  252.     /* jump to tss */
  253.     _set_break_DPMI();
  254.     hook_dpmi();
  255.     longjmp(load_state, load_state->__eax);
  256.     /* we never return here, execption routine will longjump */
  257.   }
  258.   /* exception routine:  save state, copy to tss, return */
  259.   a_tss.tss_cs = load_state->__cs;
  260.   a_tss.tss_ss = load_state->__ss;
  261.   a_tss.tss_ds = load_state->__ds;
  262.   a_tss.tss_es = load_state->__es;
  263.   a_tss.tss_fs = load_state->__fs;
  264.   a_tss.tss_gs = load_state->__gs;
  265.   a_tss.tss_eip = load_state->__eip;
  266.   a_tss.tss_esp = load_state->__esp;
  267.   a_tss.tss_eflags = load_state->__eflags;
  268.   a_tss.tss_eax = load_state->__eax;
  269.   a_tss.tss_ebx = load_state->__ebx;
  270.   a_tss.tss_ecx = load_state->__ecx;
  271.   a_tss.tss_edx = load_state->__edx;
  272.   a_tss.tss_esi = load_state->__esi;
  273.   a_tss.tss_edi = load_state->__edi;
  274.   a_tss.tss_ebp = load_state->__ebp;
  275.   a_tss.tss_irqn = load_state->__signum;
  276.   a_tss.tss_error = load_state->__sigmask;
  277.   unhook_dpmi();
  278.   _clear_break_DPMI();
  279. }
  280.  
  281. static int invalid_addr(unsigned a, unsigned len)
  282. {
  283.   /* Here we assume expand up writable code.  We could check the rights to
  284.      be sure, but that's a waste unless *_child routines fixed to know about
  285.      different selectors. */
  286.  
  287.   unsigned limit;
  288.   limit = __dpmi_get_segment_limit(__djgpp_app_DS);
  289.   if(a >= 4096 && (a+len-1) <= limit)
  290.     return 0;
  291. /*  printf("Invalid access to child, address %#x length %#x  limit: %#x\n", a, len, limit);
  292.   if (can_longjmp)
  293.     longjmp(debugger_jmpbuf, 1); */
  294.   return 1;
  295. }
  296.  
  297. int read_child(unsigned child_addr, void *buf, unsigned len)
  298. {
  299.   if (invalid_addr(child_addr, len))
  300.     return 1;
  301.   movedata(__djgpp_app_DS, child_addr, my_ds, (int)buf, len);
  302.   return 0;
  303. }
  304.  
  305. int write_child(unsigned child_addr, void *buf, unsigned len)
  306. {
  307.   if (invalid_addr(child_addr, len))
  308.     return 1;
  309.   movedata(my_ds, (int)buf, __djgpp_app_DS, child_addr, len);
  310.   return 0;
  311. }
  312.  
  313. void edi_init(jmp_buf start_state)
  314. {
  315.   my_ds = 0;
  316.   asm("mov %%ds,%0" : "=g" (my_ds) );
  317.   my_cs = 0;
  318.   asm("mov %%cs,%0" : "=g" (my_cs) );
  319.  
  320.   *load_state = *start_state;
  321.   a_tss.tss_cs = load_state->__cs;
  322.   a_tss.tss_ss = load_state->__ss;
  323.   a_tss.tss_ds = load_state->__ds;
  324.   a_tss.tss_es = load_state->__es;
  325.   a_tss.tss_fs = load_state->__fs;
  326.   a_tss.tss_gs = load_state->__gs;
  327.   a_tss.tss_eip = load_state->__eip;
  328.   a_tss.tss_esp = load_state->__esp;
  329.   a_tss.tss_eflags = load_state->__eflags;
  330.  
  331.   __djgpp_app_DS = a_tss.tss_ds;
  332.   app_cs = a_tss.tss_cs;
  333.   edi.app_base = 0;
  334.   signal(SIGTRAP, dbgsig);
  335.   signal(SIGSEGV, dbgsig);
  336.   signal(SIGFPE, dbgsig);
  337.   signal(SIGINT, dbgsig);
  338. }
  339.